package com.leetcode.LC;

import java.util.ArrayList;

public class LC1417 {
    public String reformat(String s) {
        //将原字符串拆分数字串和字母串，长度相差小于2则符合条件
        ArrayList<Character> listNum = new ArrayList<>();
        ArrayList<Character> listStr = new ArrayList<>();

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) <= 'z' && s.charAt(i) >= 'a') {
                listStr.add(s.charAt(i));
            } else {
                listNum.add(s.charAt(i));
            }
        }

        StringBuilder builder = new StringBuilder();
        if (Math.abs(listNum.size() - listStr.size()) < 2) {
            if (listNum.size() > listStr.size()) {
                for (int i = 0; i < listNum.size(); i++) {
                    builder.append(listNum.get(i));
                    if (i < listStr.size()) {
                        builder.append(listStr.get(i));
                    }
                }
            } else {
                for (int i = 0; i < listStr.size(); i++) {
                    builder.append(listStr.get(i));
                    if (i < listNum.size()) {
                        builder.append(listNum.get(i));
                    }
                }
            }
            return builder.toString();
        } else {
            return "";
        }
    }

    public static void main(String[] args) {
        System.out.println(new LC1417().reformat("ab123"));
    }
}
